package com.gxa.controller.parking;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.entity.parking.Parking;
import com.gxa.service.parking.ParkingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "停车场列表")
@RestController
@RequestMapping("/parking")
public class ParkingController {
    @Autowired
    private ParkingService parkingService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("设备停车场接口")
    @PostMapping("/list")
    public Result<PageInfo<Parking>> queryAll(@RequestBody Parking parking,Integer pageNum,Integer pageSize){
        PageInfo<Parking> parkingPageInfo = null;
        try {
            parkingPageInfo = parkingService.selectAll(parking, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(parkingPageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增停车场接口")
    @PostMapping("/add")
    public Result add(@RequestBody Parking parking){
        try {
            parkingService.add(parking);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("添加失败");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除停车场接口")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try {
            parkingService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("删除失败");
        }
        return Result.success();
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("根据id查询停车场接口")
    @GetMapping("/queryById/{id}")
    public Result queryById(@PathVariable("id") Integer id){
        Parking parking;
        try {
            parking = parkingService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(parking);
    }
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑停车场接口")
    @PutMapping("/edit")
    public Result edit(@RequestBody Parking parking){
        try {
            parkingService.update(parking);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("修改失败");
        }
        return Result.success();
    }

}
